package com.amazon.inapp.purchasing;

import android.os.RemoteException;
import com.amazon.android.framework.exception.KiwiException;
import com.amazon.venezia.command.SuccessResult;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

final class KiwiPurchaseUpdatesCommandTask extends KiwiBaseCommandTask
{
  private static final String COMMAND_NAME = "purchase_updates";
  private static final String COMMAND_VERSION = "1.0";
  private static final String TAG = "KiwiPurchaseUpdatesCommandTask";
  private final Offset _offset;

  KiwiPurchaseUpdatesCommandTask(Offset paramOffset, String paramString)
  {
    super("purchase_updates", "1.0", paramString);
    this._offset = paramOffset;
    if (Offset.BEGINNING.equals(this._offset));
    for (Object localObject = null; ; localObject = this._offset.toString())
    {
      addCommandData("cursor", localObject);
      return;
    }
  }

  private PurchaseUpdatesResponse getFailedResponse()
  {
    return new PurchaseUpdatesResponse(getRequestId(), null, PurchaseUpdatesResponse.PurchaseUpdatesRequestStatus.FAILED, null, null, this._offset, false);
  }

  private void notifyObserver(final PurchaseUpdatesResponse paramPurchaseUpdatesResponse)
  {
    postRunnableToMainLooper(new Runnable()
    {
      public void run()
      {
        PurchasingObserver localPurchasingObserver = PurchasingManager.getPurchasingObserver();
        if (localPurchasingObserver != null)
        {
          if (Logger.isTraceOn())
            Logger.trace("KiwiPurchaseUpdatesCommandTask", "Invoking onPurchaseUpdatesResponse with " + paramPurchaseUpdatesResponse);
          localPurchasingObserver.onPurchaseUpdatesResponse(paramPurchaseUpdatesResponse);
        }
      }
    });
  }

  protected void onSuccess(SuccessResult paramSuccessResult)
    throws RemoteException, KiwiException
  {
    if (Logger.isTraceOn())
      Logger.trace("KiwiPurchaseUpdatesCommandTask", "onSuccess");
    Map localMap = paramSuccessResult.getData();
    if (Logger.isTraceOn())
      Logger.trace("KiwiPurchaseUpdatesCommandTask", "data: " + localMap);
    String str1 = (String)localMap.get("errorMessage");
    String str2 = (String)localMap.get("userId");
    if (Logger.isTraceOn())
      Logger.trace("KiwiPurchaseUpdatesCommandTask", "onSuccess: errorMessage: \"" + str1 + "\"");
    if (isNullOrEmpty(str1))
      while (true)
      {
        int i;
        try
        {
          HashSet localHashSet1 = new HashSet();
          HashSet localHashSet2 = new HashSet();
          JSONArray localJSONArray1 = new JSONArray((String)localMap.get("receipts"));
          i = 0;
          if (i < localJSONArray1.length())
          {
            JSONObject localJSONObject = localJSONArray1.getJSONObject(i);
            Receipt localReceipt = getReceiptFromReceiptJson(localJSONObject);
            if (verifyReceipt(str2, localReceipt, localJSONObject))
              localHashSet1.add(localReceipt);
          }
          else
          {
            JSONArray localJSONArray2 = new JSONArray((String)localMap.get("revocations"));
            int j = 0;
            if (j < localJSONArray2.length())
            {
              localHashSet2.add(localJSONArray2.getString(j));
              j++;
              continue;
            }
            String str3 = (String)localMap.get("cursor");
            boolean bool = "true".equalsIgnoreCase((String)localMap.get("hasMore"));
            if (isNullOrEmpty(str3))
            {
              localObject = Offset.BEGINNING;
              PurchaseUpdatesResponse.PurchaseUpdatesRequestStatus localPurchaseUpdatesRequestStatus = PurchaseUpdatesResponse.PurchaseUpdatesRequestStatus.SUCCESSFUL;
              notifyObserver(new PurchaseUpdatesResponse(getRequestId(), str2, localPurchaseUpdatesRequestStatus, localHashSet1, localHashSet2, (Offset)localObject, bool));
              return;
            }
            Offset localOffset = Offset.fromString(str3);
            Object localObject = localOffset;
            continue;
          }
        }
        catch (JSONException localJSONException)
        {
          if (Logger.isErrorOn())
            Logger.error("KiwiPurchaseUpdatesCommandTask", "Error parsing purchase updates JSON: " + localJSONException.getMessage());
          sendFailedResponse();
          return;
        }
        i++;
      }
  }

  protected void sendFailedResponse()
  {
    notifyObserver(getFailedResponse());
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.inapp.purchasing.KiwiPurchaseUpdatesCommandTask
 * JD-Core Version:    0.6.2
 */